
package screens
{
	import com.greensock.plugins.TintPlugin;
	import Loadtext.LoadText;
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import objects.Bartrack;
	import objects.BaseBuild;
	import objects.BaseBuild;
	import objects.Canon;
	import objects.Enemy;
	import objects.LoadEnemy;
	import objects.MyObject;
	import objects.Path;
	import objects.Solider;
	import objects.SoliderChinese;
	import objects.TestAnimation;
	import objects.Tower;
	import objects.updateStarListItem;
	import starling.animation.DelayedCall;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.VAlign;
	import starling.utils.HAlign;
	import Assets;
	import objects.ManagerObject;
	import objects.ManagerEffect;
	import events.NavigationEvent;
	import objects.Road;
	import starling.filters.BlurFilter;
	
	/**
	 * ...
	 * @author Tuan
	 */
	public class UpdateStar extends Sprite
	{
		public var m_testText:TextField;
		public var m_background:Image;
		private var m_listUpdateStar:Array;
		public var m_ImageButtonBack:Image;
		public var m_starTotal:int;
		public var imageStarTotal:Image;
		public var textStarTotal:TextField;
		public var m_testInit:Boolean;
		public var m_testStart:Boolean;
		
		public function UpdateStar()
		{
			super();
			// gan starTotal o day. chua lay database nen lay la 9
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(starling.events.Event.ENTER_FRAME, update);
			this.addEventListener(TouchEvent.TOUCH, onTouch);
			textStarTotal = new TextField(150, 100, "", "MyFontName", 50, 0x220000);
		
		}
		
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		
		}
		
		private function onTouch(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);
			if (touch && m_testStart == true)
			{
				var m_touchPoint:Point = new Point(touch.globalX, touch.globalY);
				if (touch.phase == TouchPhase.BEGAN)
				{
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "UpdateStar-ChooseLevel"}, true));
					}
				}
				else if (touch.phase == TouchPhase.HOVER)
				{
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageButtonBack.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageButtonBack.filter = null;
				}
			}
		}
		
		public function init():void
		{
			m_starTotal = database.instance.RESULTLEVELUPDATESTAR[6];
			m_background = new Image(Assets.getTexture("background7"));
			m_ImageButtonBack = new Image(Assets.getTexture("CHUAHOANTHANH"));
			this.addChild(m_background);
			imageStarTotal = new Image(Assets.getSpriteSheet().getTexture("UpdateStarIcon_0"));
			imageStarTotal.x = 480;
			imageStarTotal.y = 200;
			//this.addChild(imageStarTotal);
			this.addChild(textStarTotal);
			textStarTotal.x = imageStarTotal.x - 35;
			textStarTotal.y = imageStarTotal.y - 25;
			textStarTotal.text = "" + m_starTotal;
			m_listUpdateStar = new Array();
			ManagerObject.Instance().setListUpdateStar(m_listUpdateStar);
			m_listUpdateStar = ManagerObject.Instance().getListUpdateStar();
			// moi skill la mot cot, o day add 2 skill. 
			var _itemIndex1:Array = new Array(4, 14, 11, 8, 6);
			var _itemIndex2:Array = new Array(0, 13, 10, 7, 2);
			var _itemIndex3:Array = new Array(1, 12, 9, 5, 3);
			m_listUpdateStar.push(new updateStarListItem(this, new Point(163, 635), 1, "base1", _itemIndex1)); // so 1 la skill 1
			m_listUpdateStar.push(new updateStarListItem(this, new Point(392, 635), 2, "base3", _itemIndex2)); // so 2 la skill 2
			m_listUpdateStar.push(new updateStarListItem(this, new Point(642, 635), 3, "base2", _itemIndex3)); // so 2 la skill 3
			
			//set level o day
			m_listUpdateStar[0].m_cicleItem.m_level = database.instance.RESULTLEVELUPDATESTAR[0]; //cot 1(skill 1), lv=1( lv mac dinh la 0)
			m_listUpdateStar[1].m_cicleItem.m_level = database.instance.RESULTLEVELUPDATESTAR[1]; //cot 2(skill 1), lv=2
			m_listUpdateStar[2].m_cicleItem.m_level = database.instance.RESULTLEVELUPDATESTAR[2]; //cot 2(skill 1), lv=3
			
			for (var i:int = 0; i < m_listUpdateStar.length; i++)
			{
				m_listUpdateStar[i].m_cicleItem.setStageInfo();
			}
			m_ImageButtonBack.pivotX = m_ImageButtonBack.width / 2;
			m_ImageButtonBack.pivotY = m_ImageButtonBack.height / 2;
			m_ImageButtonBack.width = 50;
			m_ImageButtonBack.height = 50;
			m_ImageButtonBack.x = 1025;
			m_ImageButtonBack.y = 25;
			this.addChild(m_ImageButtonBack);
			if (database.instance.m_testPlayMusic == true)
				database.instance.m_Music.playSound("Update_Music", 1.0, 999);
		}
		
		//update lai vao database o day. 
		public function updateValue(index:int):void
		{
			database.instance.setDataLevelUpdateStar(m_listUpdateStar[0].m_cicleItem.m_level, m_listUpdateStar[1].m_cicleItem.m_level, m_listUpdateStar[2].m_cicleItem.m_level, 0, 0, 0, m_starTotal);
			//cot thu nhat
			if (index == 0)
			{
				
				//skill 1
				if (m_listUpdateStar[index].m_cicleItem.m_result == 0)
				{
					//tam thoi` comment lai
					database.instance.setDataArcherPercentSell();
				}
				//skill 2
				if (m_listUpdateStar[index].m_cicleItem.m_result == 1)
				{
					database.instance.setDataArcherRange();
				}
				//skill 3
				if (m_listUpdateStar[index].m_cicleItem.m_result == 2)
				{
					database.instance.setDataArcherDeso();
				}
				//skill 4
				if (m_listUpdateStar[index].m_cicleItem.m_result == 3)
				{
					database.instance.setDataArcherMoreRange();
				}
				
				//skill 5
				if (m_listUpdateStar[index].m_cicleItem.m_result == 4)
				{
					database.instance.setDataArcherDouble();
				}
				
			}
			//cot thu 2
			if (index == 1)
			{
				//skill 1
				if (m_listUpdateStar[index].m_cicleItem.m_result == 0)
				{
					database.instance.setDataBartrack();
				}
				//skill 2
				if (m_listUpdateStar[index].m_cicleItem.m_result == 1)
				{
					database.instance.setDataBartrackArmor();
				}
				//skill 3
				if (m_listUpdateStar[index].m_cicleItem.m_result == 2)
				{
					database.instance.setDataBartrackResRange();
				}
				//skill 4
				if (m_listUpdateStar[index].m_cicleItem.m_result == 3)
				{
					database.instance.setDataBartrackMoreHp();
				}
				
				//skill 5
				if (m_listUpdateStar[index].m_cicleItem.m_result == 4)
				{
					
				}
			}
			if (index == 2)
			{
				if (m_listUpdateStar[index].m_cicleItem.m_result == 0)
				{
					database.instance.setDataCanonDam();
				}
				if (m_listUpdateStar[index].m_cicleItem.m_result == 1)
				{
					database.instance.setDataCanonRange();
				}
				if (m_listUpdateStar[index].m_cicleItem.m_result == 2)
				{
					database.instance.setDataCanonCoin();
					;
				}
			}
		
		}
		
		private function update(e:Event):void
		{
			if (m_testInit == true)
			{
				init();
				m_testInit = false;
				setDefaultSprite();
			}
			if (m_testStart == true)
			{
				ZoomSprite();
				textStarTotal.color = 0x220000;
				imageStarTotal.visible = true;
				for (var i:int = 0; i < m_listUpdateStar.length; i++)
				{
					m_listUpdateStar[i].update(Global.FRAME);
				}
				for (i = 0; i < m_listUpdateStar.length; i++)
				{
					for (var j:int = 0; j < m_listUpdateStar[i].m_cicleItem.m_baseItem.length; j++)
					{
						if (m_listUpdateStar[i].m_cicleItem.m_result != -1 && m_listUpdateStar[i].m_cicleItem.m_baseItem[m_listUpdateStar[i].m_cicleItem.m_result].m_stage == 0)
						{
							if (m_starTotal < m_listUpdateStar[i].m_cicleItem.m_baseItem[m_listUpdateStar[i].m_cicleItem.m_result].m_star)
							{
								textStarTotal.color = 0x220000;
								imageStarTotal.visible = false;
							}
							m_starTotal = m_listUpdateStar[i].levelUp(m_starTotal, m_listUpdateStar[i].m_cicleItem.m_baseItem[m_listUpdateStar[i].m_cicleItem.m_result].m_star);
							textStarTotal.text = "" + m_starTotal;
							
							updateValue(i);
							m_listUpdateStar[i].m_cicleItem.m_result = -1;
							
						}
					}
				}
			}
		}
		
		private function ZoomSprite():void
		{
			if (this.scaleX < 1)
			{
				this.scaleX += 0.05;
				this.scaleY = this.scaleX;
			}
			else
			{
				this.scaleX = 1;
				this.scaleY = 1;
			}
		}
		
		private function setDefaultSprite():void
		{
			//this.width = 1050;
			//this.height = 700;
			//this.pivotX = this.width / 2;
			//this.pivotY = this.height / 2;
			//this.x = this.width / 2;
			//this.y = this.height / 2;
		}
		
		public function disposeTemporarily():void
		{
			database.instance.m_Music.stopSound("Update_Music");
			this.removeChildren(0, this.numChildren, false);
			this.scaleX = 0.1;
			this.scaleY = this.scaleX;
			this.visible = false;
			this.m_testStart = false;
		}
	}
}